import { api } from '../utils/api.js'

// 认证相关API
export const authApi = {
  // 登录
  login: (credentials) => api.post('/api/auth/login/', credentials),
  
  // 注册
  register: (userData) => api.post('/api/auth/register/', userData),
  
  // 注销
  logout: () => api.post('/api/auth/logout/'),
  
  // 获取用户信息
  getUserInfo: () => api.get('/api/auth/user/'),
  
  // 刷新token
  refreshToken: (refreshToken) => api.post('/api/auth/refresh/', { refresh: refreshToken })
}

// 戒烟档案API
export const quitProfileApi = {
  // 获取用户的戒烟档案
  getProfile: () => api.get('/api/quit-profile/'),
  
  // 创建戒烟档案
  createProfile: (profileData) => api.post('/api/quit-profile/', profileData),
  
  // 更新戒烟档案
  updateProfile: (profileData) => api.put('/api/quit-profile/', profileData),
  
  // 获取戒烟统计数据
  getStats: () => api.get('/api/quit-profile/stats/'),
  
  // 删除戒烟档案
  deleteProfile: () => api.delete('/api/quit-profile/')
}

// 每日记录API
export const dailyRecordApi = {
  // 获取每日记录列表
  getRecords: (params = {}) => api.get('/api/daily-records/', { params }),
  
  // 获取指定日期的记录
  getRecord: (date) => api.get(`/api/daily-records/${date}/`),
  
  // 创建每日记录
  createRecord: (recordData) => api.post('/api/daily-records/', recordData),
  
  // 更新每日记录
  updateRecord: (date, recordData) => api.put(`/api/daily-records/${date}/`, recordData),
  
  // 删除每日记录
  deleteRecord: (date) => api.delete(`/api/daily-records/${date}/`),
  
  // 获取最近的记录
  getRecentRecords: (days = 7) => api.get(`/api/daily-records/recent/?days=${days}`),
  
  // 获取记录统计
  getRecordStats: (startDate, endDate) => api.get(`/api/daily-records/stats/?start_date=${startDate}&end_date=${endDate}`)
}

// 里程碑API
export const milestoneApi = {
  // 获取所有里程碑
  getMilestones: () => api.get('/api/milestones/'),
  
  // 获取特定里程碑
  getMilestone: (id) => api.get(`/api/milestones/${id}/`),
  
  // 获取用户可达成的里程碑
  getAchievableMilestones: () => api.get('/api/milestones/achievable/'),
  
  // 获取即将达成的里程碑
  getUpcomingMilestones: () => api.get('/api/milestones/upcoming/')
}

// 用户成就API
export const userMilestoneApi = {
  // 获取用户的所有成就
  getUserMilestones: () => api.get('/api/user-milestones/'),
  
  // 获取用户成就详情
  getUserMilestone: (id) => api.get(`/api/user-milestones/${id}/`),
  
  // 手动标记里程碑达成
  achieveMilestone: (milestoneId) => api.post('/api/user-milestones/', { milestone_id: milestoneId }),
  
  // 获取用户成就统计
  getAchievementStats: () => api.get('/api/user-milestones/stats/'),
  
  // 检查新成就
  checkNewAchievements: () => api.get('/api/user-milestones/check-new/')
}

// 健康建议API
export const healthTipApi = {
  // 获取所有健康建议
  getHealthTips: (params = {}) => api.get('/api/health-tips/', { params }),
  
  // 获取特定类别的建议
  getTipsByCategory: (category) => api.get(`/api/health-tips/?category=${category}`),
  
  // 获取随机建议
  getRandomTip: () => api.get('/api/health-tips/random/'),
  
  // 获取每日建议
  getDailyTip: () => api.get('/api/health-tips/daily/'),
  
  // 标记建议为有用
  markTipHelpful: (tipId) => api.post(`/api/health-tips/${tipId}/helpful/`),
  
  // 获取推荐建议
  getRecommendedTips: () => api.get('/api/health-tips/recommended/')
}

// 烟瘾记录API
export const cravingLogApi = {
  // 获取烟瘾记录列表
  getCravingLogs: (params = {}) => api.get('/api/craving-logs/', { params }),
  
  // 获取特定烟瘾记录
  getCravingLog: (id) => api.get(`/api/craving-logs/${id}/`),
  
  // 创建烟瘾记录
  createCravingLog: (logData) => api.post('/api/craving-logs/', logData),
  
  // 更新烟瘾记录
  updateCravingLog: (id, logData) => api.put(`/api/craving-logs/${id}/`, logData),
  
  // 删除烟瘾记录
  deleteCravingLog: (id) => api.delete(`/api/craving-logs/${id}/`),
  
  // 获取烟瘾统计
  getCravingStats: (startDate, endDate) => api.get(`/api/craving-logs/stats/?start_date=${startDate}&end_date=${endDate}`),
  
  // 获取最近的烟瘾记录
  getRecentCravings: (days = 7) => api.get(`/api/craving-logs/recent/?days=${days}`),
  
  // 获取触发因素统计
  getTriggerStats: () => api.get('/api/craving-logs/triggers/'),
  
  // 快速记录烟瘾
  quickLog: (intensity, trigger) => api.post('/api/craving-logs/quick/', { intensity, trigger })
}

// 仪表板API
export const dashboardApi = {
  // 获取仪表板数据
  getDashboardData: () => api.get('/api/dashboard/'),
  
  // 获取戒烟进度
  getQuitProgress: () => api.get('/api/dashboard/progress/'),
  
  // 获取今日摘要
  getTodaySummary: () => api.get('/api/dashboard/today/'),
  
  // 获取周报
  getWeeklyReport: () => api.get('/api/dashboard/weekly/'),
  
  // 获取月报
  getMonthlyReport: () => api.get('/api/dashboard/monthly/')
}

// 统计API
export const statsApi = {
  // 获取整体统计
  getOverallStats: () => api.get('/api/stats/'),
  
  // 获取心情趋势
  getMoodTrend: (days = 30) => api.get(`/api/stats/mood-trend/?days=${days}`),
  
  // 获取烟瘾强度趋势
  getCravingTrend: (days = 30) => api.get(`/api/stats/craving-trend/?days=${days}`),
  
  // 获取成功率统计
  getSuccessRate: () => api.get('/api/stats/success-rate/'),
  
  // 获取节省金额趋势
  getMoneySavedTrend: (days = 30) => api.get(`/api/stats/money-saved/?days=${days}`)
}

// AI助手API
export const aiAssistantApi = {
  // 发送消息给AI助手
  sendMessage: (messageData) => api.post('/api/ai-assistant/chat/', messageData),
  
  // 获取对话历史
  getChatHistory: (params = {}) => api.get('/api/ai-assistant/history/', { params }),
  
  // 获取特定对话会话
  getChatSession: (sessionId) => api.get(`/api/ai-assistant/${sessionId}/`),
  
  // 保存对话消息
  saveChatMessage: (messageData) => api.post('/api/ai-assistant/save/', messageData),
  
  // 获取知识库信息
  getKnowledgeBase: (query) => api.get(`/api/ai-assistant/knowledge/?q=${query}`),
  
  // 评价AI回复
  rateResponse: (responseId, rating) => api.post(`/api/ai-assistant/rate/${responseId}/`, { rating }),
  
  // 获取AI助手统计
  getStats: () => api.get('/api/ai-assistant/stats/')
}
// 导出所有API
export default {
  auth: authApi,
  quitProfile: quitProfileApi,
  dailyRecord: dailyRecordApi,
  milestone: milestoneApi,
  userMilestone: userMilestoneApi,
  healthTip: healthTipApi,
  cravingLog: cravingLogApi,
  dashboard: dashboardApi,
  stats: statsApi,
  aiAssistant: aiAssistantApi
}